package com.lightheart.sphr.xiaobinggan.gps

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.widget.Toast
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.base.BaseActivity
import com.lightheart.sphr.xiaobinggan.module.home.presenter.HomePresenter
import kotlinx.android.synthetic.main.activity_gps.*
import kotlinx.android.synthetic.main.common_toolbar.*


class GpsActivity : BaseActivity<HomePresenter>() {


    override fun getLayoutId() = R.layout.activity_gps

    override fun initInjector() {
    }

    override fun initView() {
        initToolbar(common_toolbar, common_toolbar_title_tv, bt_sub, R.string.gps, false, R.string.register)
        tv.setOnClickListener {
            if (!GpsUtil.isOPen(this)) {
                openGPS()
            } else {
                gotoAppDetailIntent(this)
//                Toast.makeText(this, "定位是开启状态", Toast.LENGTH_LONG).show()
            }
        }
    }

    fun gotoAppDetailIntent(activity: Activity) {//安卓跳转到应用信息界面(用于方便用户打开权限)
        val intent = Intent()
        intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
        intent.data = Uri.parse("package:" + activity.packageName)
        activity.startActivityForResult(intent,857)
    }

    private fun openGPS() {
        AlertDialog.Builder(this@GpsActivity)
                .setTitle("定位失败")
                .setMessage("打开定位服务来获取金豆")
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(
                        R.string.open) { dialogInterface, _ ->
                    val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                    startActivityForResult(intent, 887)
                    dialogInterface.dismiss()
                }
                .show()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
            887 -> { //开启GPS，重新添加地理监听
                Toast.makeText(this, "定位以开启", Toast.LENGTH_LONG).show()
            }
            857->{
                Toast.makeText(this, "666666", Toast.LENGTH_LONG).show()
            }
            else -> {
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun showHomeAsUp(): Boolean {
        return true
    }

}